home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_300
/
355_02
/
slk2.exe
/
SPP
/
SYS.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-06-09
|
19KB
|
940 lines
/*
New Sherlock Preprocessor -- system module.
Source: sys.c
Started: October 7, 1985
Version:
July 20, 1988;
November 1, 1988 bug fix: extra '\r' in sysnlput().
February 10, 1989 bug fix: kludge in sysopen().
February 16, 1989
periods removed from error messages.
t_line bug fixed in sysopen().
June 22, 1989
backslash newlines now filtered by sysnext().
August 3, 1989
signal code added to syscsts().
errors written to stderr.
PUBLIC DOMAIN SOFTWARE
Sherlock, including the SPP, SDEL and SDIF programs, was placed in
the public domain on June 15, 1991, by its author,
Edward K. Ream
166 North Prospect Ave.
Madison, WI 53705.
(608) 257-0802
Sherlock may be used for any commercial or non-commercial purpose.
DISCLAIMER OF WARRANTIES
Edward K. Ream (Ream) specifically disclaims all warranties,
expressed or implied, with respect to this computer software,
including but not limited to implied warranties of merchantability
and fitness for a particular purpose. In no event shall Ream be
liable for any loss of profit or any commercial damage, including
but not limited to special, incidental consequential or other damages.
*/
#include "spp.h"
#ifdef MICRO_SOFT
#include <signal.h>
#include <fcntl.h>
#include <sys\types.h>
#include <sys\stat.h>
#include <io.h>
#include <time.h>
#endif
#ifdef TURBOC
#include <signal.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <io.h>
#include <conio.h>
#include <time.h>
#endif
/*
Define the format of a file node.
The node contains all information pertaining to the file.
The fil_name and f_line are used to update preprocessor globals.
*/
typedef enum { NULL_STAT,
CLOSED_STAT, INPUT_STAT, OUTPUT_STAT, LOCAL_STAT, EOF_STAT
} en_stat;
static struct FN {
struct FN * f_next; /* pointer to next node */
char * fil_name; /* file name */
int f_line; /* line number */
char * f_buffer; /* file buffer */
int f_bufsize; /* size of file buffer: bytes */
int f_handle; /* handle to file. */
char * f_bufp; /* pointer into file buffer */
int f_bufc; /* characters in buffer */
en_stat f_type; /* status of file */
int f_lastc; /* restart character */
};
#define FN_SIZE (sizeof(struct FN))
static struct FN * in_list = NULL; /* List of input nodes. */
static struct FN * outn_list = NULL; /* List of output nodes. */
/*
The following global variables are copies of the fields in FN.
Global variables are used as an efficiency measure.
*/
static char * s_ip = NULL; /* Copy of f_bufp for input file. */
static int s_ic = 0; /* Copy of f_bufc for input file. */
static char * s_op = NULL; /* Copy of f_bufp for output file. */
static int s_oc = 0; /* Copy of f_bufc for output file. */
#define MAX_INLEVEL 20 /* Max depth of nested #includes. */
#define IBUF_SIZE 2048 /* Size of each input buffer. */
#define OBUF_SIZE 2048 /* Size of the output buffer. */
/*
Define "pushed back" characters used by sysn1().
*/
static int push_back = -1; /* Used by macro logic. */
static int file_put_back = -1; /* Used by non-macro logic. */
/*
Define variables used by time and date routines.
*/
static long ltime;
static struct tm *newtime;
static char time_buf [30];
static char date_buf [30];
/*
Declare static functions in this module.
*/
static void raw_close (int);
static int raw_creat (char *);
static int raw_open (char *);
static int raw_read (int, char *, int);
static int raw_write (int, char *, int);
static int syscstat (void);
static void sysn1 (void);
static struct FN *sys_new (int);
static void sys_release (void);
/*
Close a file opened with raw_open() or raw_creat().
*/
static void
raw_close(handle)
int handle;
{
TRACEP("raw_close", printf("(handle: %d)\n", handle));
close (handle);
}
/*
Open the file for writing only.
Return a handle (int) or ERROR.
*/
static int
raw_creat(name)
char *name;
{
TRACEP("raw_creat", printf("(%s)\n", name));
chmod(name, S_IREAD | S_IWRITE);
return creat(name, S_IREAD | S_IWRITE);
}
/*
Open the file for reading only.
Return a handle (int) or ERROR.
*/
static int
raw_open(name)
char *name;
{
TRACEP("raw_open", printf("(%s)\n", name));
return open(name, O_RDONLY | O_BINARY);
}
/*
Read n bytes from the file described by handle into buffer[].
Return the number of bytes read.
*/
static int
raw_read(handle, buffer, n)
int handle;
char *buffer;
int n;
{
int result;
TRACEP("raw_read", printf("(handle: %d, buffer: %lx, n: %d)\n",
handle, buffer, n));
result = read (handle, buffer, n);
TRACEP("raw_read", printf("returns %d\n", result));
return result;
}
/*
Write n bytes from buffer[] to the file described by handle.
Return the number of bytes written.
*/
static int
raw_write(handle, buffer, n)
int handle;
char *buffer;
int n;
{
TRACEP("raw_write", printf("(handle: %dx, buffer: %lx, n: %d)\n",
handle, buffer, n));
return write (handle, buffer, n);
}
/*
Close all files and exit.
Do not call fatal() or sysabort() from inside
sysiclose(), sysoclose(), or sys_release().
*/
void
sysabort()
{
/* Close the output file. */
if (outn_list != NULL) {
sysoclose();
}
/* Close all input files. */
while(in_list != NULL) {
sysiclose();
}
sysend();
exit(BAD_EXIT);
}
/*
Put a non-newline to the output file.
This is the second most called routine after sysnext().
*/
void
syscput(c)
char c;
{
struct FN * fnp;
*s_op++ = c;
s_oc++;
if (s_oc == OBUF_SIZE) {
fnp = outn_list;
if (raw_write(fnp->f_handle, fnp->f_buffer, OBUF_SIZE) !=
OBUF_SIZE) {
error("Disk full");
return;
}
s_oc = 0;
s_op = fnp -> f_buffer;
}
}
/*
Open a file for output. Only one such file may be open at a time.
Return TRUE if all went well.
*/
bool
syscreat(name)
register char * name;
{
register struct FN * fnp;
int handle;
TRACEP("syscreat", printf("(%s)\n", name));
fnp = outn_list;
if (fnp == NULL) {
/* Allocate node for output file. */
fnp = outn_list = sys_new(OBUF_SIZE);
}
else if (fnp -> f_type != CLOSED_STAT) {
syserr("syscreat: Can't happen");
}
/* Actually open the file. */
handle = raw_creat(name);
if (handle == ERROR) {
return FALSE;
}
fnp -> f_handle = handle;
fnp -> f_type = OUTPUT_STAT;
/* The output buffer is empty. */
s_oc = 0;
s_op = fnp -> f_buffer;
return TRUE;
}
/*
Return 0 if no character is ready from the keyboard.
Otherwise, return the character itself.
*/
static int sys_inited = 0;
/*
Interrupt handler for control-c.
*/
static void
ctrl_handler(int sig)
{
fprintf(stderr, "\nSPP terminated by operator\n");
exit(0);
}
void
syscsts()
{
if (sys_inited == 0) {
sys_inited = 1;
if (signal(SIGINT, ctrl_handler) == SIG_ERR) {
fprintf(stderr, "\nsignal failed.\n");
exit(-1);
sysabort();
}
}
}
#if 0 /* Machine independent version. Old code. */
static int
syscstat()
{
if (kbhit()) {
return fgetchar() & 0x7f;
}
else {
return 0;
}
}
/*
Get console status and pause if the user has hit control S.
Abort if user has hit control C.
*/
#define CONTROL_C 3
void
syscsts()
{
int c;
c = syscstat();
if (c == CONTROL_C) {
fprintf(stderr, "\nSPP terminated by operator\n");
sysabort();
}
}
#endif /* old code */
/*
Return the print string of the current date.
*/
char *
sysdate()
{
char *p;
time(<ime);
newtime = localtime(<ime);
p = asctime(newtime);
if (strlen(p) != 25) {
return "";
}
else {
strcpy(date_buf, "\"");
p[11] = '\0';
strcat(date_buf, p);
p[24] = '\0';
strcat(date_buf, p + 20);
strcat(date_buf, "\"");
TRACEPN("sysdate", printf("returns: %s\n", date_buf));
return date_buf;
}
}
/*
Shut down the system. This routine is called just before doing an
exit(). Do any last minute things here.
*/
void
sysend()
{
TICK("sysend");
}
/*
Push c back into the file input stream